package org.example.auth.controller;


import org.example.auth.contract.request.AuthorityRequestForPage;
import org.example.auth.contract.request.AuthorityRequestForRoleBind;
import org.example.auth.contract.request.AuthorityRequestForSave;
import org.example.auth.contract.response.AuthorityResponse;
import org.example.auth.contract.response.AuthorityResponseForRole;
import org.example.auth.convert.AuthorityConvert;
import org.example.auth.service.AuthorityService;
import org.example.auth.service.RoleAuthorityService;
import org.example.misc.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;
    @Autowired
    private AuthorityConvert authorityConvert;
    @Autowired
    private RoleAuthorityService roleAuthorityService;

    /**
     * 保存权限
     */
    @PostMapping("/save")
    Result<AuthorityResponse> save(@RequestBody @Validated AuthorityRequestForSave request) {
        return Result.ok(authorityConvert.clone(authorityService.save(request)));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    Result<Page<AuthorityResponse>> getPage(@RequestBody @Validated AuthorityRequestForPage request, Pageable pageable) {
        return Result.ok(authorityService.getPage(request, pageable));
    }

    /**
     * 获取所有权限
     */
    @GetMapping("/getAllList")
    Result<List<AuthorityResponse>> getAllList() {
        return Result.ok(authorityConvert.clone(authorityService.getAllList()));
    }

    /**
     * 删除某个权限
     */
    @GetMapping("/deleteAuthority")
    Result<Boolean> deleteAuthority(@RequestParam Long id) {
        authorityService.deleteAuthority(id);
        return Result.ok(Boolean.TRUE);
    }

    /**
     * 获取某个角色的权限
     */
    @GetMapping("/getListByRoleId")
    Result<AuthorityResponseForRole> groupAuthorityByRoleId(@RequestParam Long roleId) {
        return Result.ok(authorityService.groupAuthorityByRoleId(roleId));
    }

    /**
     * 批量 角色的权限绑定
     */
    @PostMapping("batchBindRoleAuthority")
    Result<Boolean> batchBindRoleAuthority(@RequestBody AuthorityRequestForRoleBind request) {
        roleAuthorityService.batchBindRoleAuthority(request);
        return Result.ok(Boolean.TRUE);
    }

    /**
     * 单个角色的 权限绑定
     */
    Result<Boolean> bindRoleAuthority(@RequestParam Long roleId,@RequestParam Long authorityId){
        roleAuthorityService.bindRoleAuthority(roleId,authorityId);
        return Result.ok(Boolean.TRUE);
    }
    /**
     * 单个角色的 权限解绑
     */
    Result<Boolean> unBindRoleAuthority(@RequestParam Long roleId,@RequestParam Long authorityId){
        roleAuthorityService.unBindRoleAuthority(roleId,authorityId);
        return Result.ok(Boolean.TRUE);
    }
}
